/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.govern;

import com.wtkj.vo.dzzf.base.kfwd.VehicleInfo;
import com.wttech.dzzf.check.moduleSet.CheckVehicleUpload_9;
import com.wttech.dzzf.process.dao.DataGovernDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import com.wttech.dzzf.process.util.BusinessUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.wtkj.vo.dzzf.share.staticvalue.VehicleStaticValue.VehType.TRUCK_ONE;
import static com.wttech.dzzf.check.moduleSet.CheckVehicleUpload_9.*;
import static com.wttech.dzzf.process.constant.UserConstants.*;

/**
 * 车辆信息治理
 *
 * @author MaShibo
 * @date 2017/12/7.
 */
@Service
public class VehicleInfoGovernService {

	private static final Logger VEHICLE = LoggerFactory.getLogger("VEHICLE");

	private static final String REGISTERDATE = "REGISTERDATE";

	private static final String ISSUEDATE = "ISSUEDATE";

	private static final String FILENUM = "FILENUM";

	private static final String VEHLENGTH = "VEHLENGTH";

	private static final String VEHWIDTH = "VEHWIDTH";

	private static final String VEHHEIGHT = "VEHHEIGHT";

	private static final String WHEELBASE = "WHEELBASE";

	private static final String AXISTYPE = "AXISTYPE";

	private static final String OWNERIDTYPE = "OWNERIDTYPE";

	private static final String USECHARACTER = "USECHARACTER";

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private DataGovernDao dataGovernDao;

	@Resource
	private BaseInfoService baseInfoService;


	void governVehicleInfo(VehicleInfo vehicleInfo) {
		String vehicleInfoId = StringUtils.trim(vehicleInfo.getVehicleInfoId());
		CheckVehicleUpload_9 check = new CheckVehicleUpload_9();

		Integer type = vehicleInfo.getVehType();
		String ownerName = vehicleInfo.getOwnerName();
		Integer ownerIdType = vehicleInfo.getOwnerIdType();
		String ownerIdNum = vehicleInfo.getOwnerIdNum();
		String ownerTel = vehicleInfo.getOwnerTel();
		String contact = vehicleInfo.getContact();
		String vehicleType = vehicleInfo.getCertifiVehType();
		String vehicleModel = vehicleInfo.getBrand();
		Integer userCharacter = vehicleInfo.getUseCharacter();
		String vin = vehicleInfo.getVehiclelicNO();
		String engineNum = vehicleInfo.getVehicleEngineNO();
		Date registerDate = vehicleInfo.getRegisterDate();
		Date issueDate = vehicleInfo.getIssueDate();
		String fileNum = vehicleInfo.getFileNum();
		Integer approvedCount = vehicleInfo.getSeats();
		Integer totalMass = vehicleInfo.getWeight();
		Integer maintenanceMass = vehicleInfo.getCurbWeight();
		Integer permittedWeight = vehicleInfo.getLoadLimit();
		Integer vehlength = vehicleInfo.getVehLength();
		Integer vehWidth = vehicleInfo.getVehWidth();
		Integer vehHeight = vehicleInfo.getVehHeight();

		Integer permittedTowWeight = vehicleInfo.getPermittedTowWeight();
		String testRecord = vehicleInfo.getTestRecord();
		Integer wheelCount = vehicleInfo.getWheelCnt();
		Integer axleCount = vehicleInfo.getAxisCnt();
		Integer axleDistance = vehicleInfo.getWheelBase();
		String axisType = vehicleInfo.getAxisType();

		String result;
		VehicleInfo update = new VehicleInfo();
		update.setVehicleInfoId(vehicleInfoId);

		List<String> updateNullList = new ArrayList<>();
		boolean isUpdated = false;

		result = check.checkType(type);
		if (baseInfoService.needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，车型【{}】错误【{}】", new Object[]{vehicleInfoId, type, result});
		}

		result = checkOwnerName(ownerName);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("OWNERNAME");
		}

		int convertOwnerIdType = BusinessUtils.convertUserIdType(ownerIdType, ownerIdType != null && ownerIdType > 100 ? OLD_OWNERTYPE_COMPANY : OLD_OWNERTYPE_USER);
		result = checkOwnerIdType(convertOwnerIdType);
		if (convertOwnerIdType == DEFAULT_USER_USERIDTYPE || convertOwnerIdType == DEFAULT_COMPANY_USRERIDTYPE ||
				baseInfoService.needOutput(result)) {
			updateNullList.add(OWNERIDTYPE);
		}

		result = check.checkOwnerIdNum(ownerIdNum);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("OWNERIDNUM");
		}

		result = checkOwnerTel(ownerTel);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("OWNERIDTEL");
		}

		result = check.checkContact(contact);
		if (baseInfoService.needOutput(result)) {
			update.setContact(DEFAULT_CONTACT);
			isUpdated = true;
		}

		result = checkVehicleType(vehicleType);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("CERTIFIVEHTYPE");
		}

		result = checkVehicleModel(vehicleModel);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("BRAND");
		}

		result = checkUseCharacter(userCharacter);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add(USECHARACTER);
		}

		result = check.checkVIN(vin);
		if (baseInfoService.needOutput(result)) {
			String trimVin = StringUtils.trim(vin);
			result = check.checkVIN(trimVin);
			update.setVehiclelicNO(baseInfoService.needOutput(result) ? DEFAULT_VEHICLE_VIN : trimVin);
			isUpdated = true;
		}

		result = checkEngineNum(engineNum);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("VEHICLEENGINENO");
		}

		result = checkRegisterDate(registerDate);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add(REGISTERDATE);
		}

		result = checkIssueDate(issueDate);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add(ISSUEDATE);
		}

		result = checkFileNum(fileNum);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add(FILENUM);
		}

		result = checkApprovedCount(approvedCount);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("SEATS");
		}

		result = checkTotalMass(totalMass);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("WEIGHT");
		}

		result = checkMaintenanceMass(maintenanceMass);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("CURBWEIGHT");
		}


		result = checkPermittedWeight(permittedWeight);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("LOADLIMIT");
		}

		if (vehHeight != null && vehWidth != null && vehlength != null) {
			String outsideDimensions = vehlength + "X" + vehWidth + "X" + vehHeight;
			result = checkOutsideDimensions(outsideDimensions);
			if (baseInfoService.needOutput(result)) {
				updateNullList.add(VEHLENGTH);
				updateNullList.add(VEHWIDTH);
				updateNullList.add(VEHHEIGHT);
			}
		}

		result = checkPermittedTowWeight(permittedTowWeight);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("PERMITTEDTOWWEIGHT");
		}

		result = checkTestRecord(testRecord);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add(result);
		}

		result = checkWheelCount(wheelCount);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("WHEELCNT");
		}

		result = checkAxleCount(axleCount);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add("AXISCNT");
		}

		result = checkAxleDistance(axleDistance);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add(WHEELBASE);
		}

		result = checkAxisType(axisType);
		if (baseInfoService.needOutput(result)) {
			updateNullList.add(AXISTYPE);
		}

		if (isUpdated) {
			transferUserInfoDao.updateDzzfObject(update);
		}

		if (!updateNullList.isEmpty()) {
			dataGovernDao.updateVehInfoNulls(updateNullList, vehicleInfoId);
		}
	}


}
